package org.iks.common.pojo.message;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kzou
 * @date 2024/1/2 21:02
 * @description: 消息基类
 */
public interface Message extends Serializable {
    /**
     * 获取消息类型
     */
    @JsonIgnore
    byte getMessageType();

    /**
     * 获取序列化方式
     */
    @JsonIgnore
    default byte getSerialType(){
        return SerializationType.JSON.getCode();
    }

    /**
     * 获取序列化后的类容
     */
    @JsonIgnore
    default byte[] getSerializedData(){
        SerializationType serializationType = SerializationType.of(getSerialType());
        if (serializationType == null) {
            return null;
        }
        return serializationType.serialization(this);
    }

    /**
     * 消息类型
     */
    byte ChartMessageType = 1;
    byte AuthVerifyMessageType = 2;

    static Message deserializeData(byte[] data, byte serialType, byte action) {
        SerializationType serializationType = SerializationType.of(serialType);
        if (serializationType == null) {
            return null;
        }
        return (Message) serializationType.deserialization(data, actions.get(action));
    }

    Map<Byte, Class<?>> actions = new HashMap<>();

}
